// pages/detail/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollViewHeight: "",
    id: "",
    cat: "",
    catname: "",
    author: "",
    date: "",
    hitokoto: "",
    error: ""
  },
  onShareAppMessage: function () {

    return {

      title: '每日一言',

      desc: '看看今日心情如何',

      path: '/pages/yiyan/yiyan'

    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var that = this;

    //显示加载动画
    wx.showLoading({
      title: '正在加载',
    });

    // //获取屏幕高度
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.windowHeight);
        that.setData({
          scrollViewHeight: res.windowHeight - 100
        });
      }
    });
    //发送请求，获取每日一言
    wx.request({
      //请求api地址
      url: "https://v1.hitokoto.cn/?encode=json",
      //请求表头
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      //请求方式
      method: 'GET',
      //返回数据处理
      success: function (res) {
        //处理api返回的数据
        var msg = res.data;
        console.log('begin---');
        // console.log(msg);
        console.log("success:返回id" + res.data.id);
        console.log('---end');
        try {
          that.setData({
            id: msg['id'],
            cat: msg['hitokoto'],
            catname: "来源于： " + msg['from'],
            author: "-----by  " + msg['creator'],
            date: msg['created_at'],
            hitokoto: msg['hitokoto']
          });

        } catch (e) {
          that.setData({
            hitokoto: "网络故障，请检查网络"
          });
        }
        wx.hideLoading();
      },
      fail: function (err) {
        console.log("error:" + err);
        that.setData({
          error: "opps'.....网络故障，请检查网络"
        });
        wx.hideLoading();
      }
    });

  },
})